package com.web.common.web.session;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.css.sword.kernel.platform.SwordSession;
import com.css.sword.kernel.utils.SwordSessionUtils;
import com.css.sword.platform.web.session.ISwordSessionDataCreater;

public class SwordSessionDataMH implements ISwordSessionDataCreater {

	@Override
	public void createSessionData(HttpServletRequest request,
			SwordSession swordSession) {
		HttpSession hs = request.getSession();
		Object userId = hs.getAttribute("userId");
		Object ryxm = hs.getAttribute("ryxm");
		Object jgId = hs.getAttribute("jgId");
		Object bmid = hs.getAttribute("bmid");
		Object jgmc = hs.getAttribute("jgmc");
		Object gnIds = hs.getAttribute("gnIds");
		Object tidorctrls = hs.getAttribute("tidorctrls");
		Object gwIds = hs.getAttribute("gwIds");
		Object yhm = hs.getAttribute("yhm");
		Object jgsd = hs.getAttribute("jgsd");
		
		if (jgsd != null && !jgsd.equals("")) {
			SwordSessionUtils.putTempDataIntoApplicationContext("jgsd", jgsd);
		}
		if (bmid != null && !bmid.equals("")) {
			SwordSessionUtils.putTempDataIntoApplicationContext("bmid", bmid);
		}
		if (jgmc != null && !jgmc.equals("")) {
			SwordSessionUtils.putTempDataIntoApplicationContext("jgmc", jgmc);
		}
		if (ryxm != null && !ryxm.equals("")) {
			SwordSessionUtils.putTempDataIntoApplicationContext("ryxm", ryxm);
		}
		if (yhm != null && !yhm.equals("")) {
			SwordSessionUtils.putTempDataIntoApplicationContext("yhm", yhm);
		}
		if (userId != null && !userId.equals("")) {
			swordSession.setUserID((String)userId);
		}
		if (jgId != null && !jgId.equals("")) {
			swordSession.setOrgID((String)jgId);
		}
		if (gnIds != null && !gnIds.equals("")) {
			SwordSessionUtils.putTempDataIntoApplicationContext("gnIds", gnIds);
		}
		if (gwIds != null && !gwIds.equals("")) {
			SwordSessionUtils.putTempDataIntoApplicationContext("gwIds", gwIds);
		}
		if (tidorctrls != null && !tidorctrls.equals("")) {
			SwordSessionUtils.putTempDataIntoApplicationContext("tidorctrls", tidorctrls);
		}
		
		String gwid = getReqValue("gwid", request);
		String znid = getReqValue("znid", request);
		
		if (gwid != null && !gwid.equals("")) {
			SwordSessionUtils.putTempDataIntoApplicationContext("gwid", gwid);
		}
		if (znid != null && !znid.equals("")) {
			SwordSessionUtils.putTempDataIntoApplicationContext("znid", znid);
		}
		
	}

	private String getReqValue(String key, HttpServletRequest req) {
		String value = req.getParameter(key);
		if (value == null || value.equals("")) {
			String path = req.getHeader("referer");
			if (path != null && path.indexOf("&") != -1) {
				String[] pathData = path.substring(path.indexOf("&") + 1)
						.split("&");
				for (int i = 0; i < pathData.length; i++) {
					String[] data = pathData[i].split("=");
					for (int j = 0; j < data.length;) {
						if (key.equals(data[j])) {
							if((j + 1)< data.length){
								value = data[j + 1];
								break;
							}else{
								value="null";
								break;
							}
							
						} else {
							j += 2;
						}
					}
				}
			}
		}
		return value;
	}

	public Map<String, String> getSessionAndValidate(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		HttpSession hs = request.getSession();
		String userId = (String) hs.getAttribute("userId");
		String jgId = (String) hs.getAttribute("jgId");

		if (userId != null && !userId.equals("")) {
			map.put("userId", userId);
		}
		if (jgId != null && !jgId.equals("")) {
			map.put("jgId", jgId);
		}
		String ctrl = request.getParameter("ctrl");
		String tid = request.getParameter("tid");
		if (ctrl != null && !ctrl.equals("")) {
			map.put("ctrl", ctrl);
		}
		if (tid != null && !tid.equals("")) {
			map.put("tid", tid);
		}
		return map;
	}
}